package com.homework.action;

import java.util.HashMap;
import java.util.Map;
import com.homework.service.UserService;
import com.homework.util.connDB;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class UsernameCheckAction extends ActionSupport implements
		ServletRequestAware {
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;
	private Map<String, Object> dataMap;

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	/**
	 * ajax校验用户名是否已经存在，返回json
	 * 
	 * @return SUCCESS
	 */
	public String execute() {
		//数据库的配置文件加载：
		connDB.loadProperty();
		
		UserService userService = new UserService();
		try {
			dataMap = new HashMap<String, Object>();
			String username = request.getParameter("username");
			if (username != null) {
				if (!username.isEmpty()) {
					boolean exist = userService.usernameCheck(username);
					dataMap.put("exist", exist);
				}
			} else {
				// return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

}